package ${controllerPackage!};

import cn.roin.msf.controller.BaseController;
import cn.roin.msf.dto.PageInfo;
import cn.roin.msf.dto.Pageable;
import cn.roin.msf.dto.ResultBean;
import ${modelFullName!};
import cn.roin.msf.exception.BusinessException;
import ${servicePackage!}.${modelName!}Service;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * ${module!}-Controller
 * @author ${author!}
 * @date ${.now}
 */
@Controller
@RequestMapping("${modelSnakeName!}")
public class ${modelName!}Controller extends BaseController{

    private static final Logger log = LoggerFactory.getLogger(${modelName!}Controller.class);

    @Autowired
    ${modelName!}Service ${modelHumpName!}Service;

<#if controllerConfig.list>
    /**
     * 跳转到主页面
     * @param model
     * @param menuCode
     * @return
     */
    <#if controllerConfig.listPermission>
    @RequiresPermissions("${modelSnakeName!}:search")
    </#if>
    @RequestMapping("list.htm")
    public String toMain(Model model, String menuCode){
        setMenuButtons(model,menuCode);
        return "${modelSnakeName!}/list";
    }

    /**
     * 分页查询数据
     * @param pageable 查询参数封装类实体
     * @return
     */
    <#if controllerConfig.listPermission>
    @RequiresPermissions("${modelSnakeName!}:search")
    </#if>
    @RequestMapping("list")
    @ResponseBody
    public PageInfo<${modelName!}> list(Pageable pageable){
        PageInfo<${modelName!}> page = ${modelHumpName!}Service.getPage(pageable);
        return page;
    }
</#if>

<#if controllerConfig.add>
    /**
     * 增加数据
     * @param ${modelHumpName!}
     * @return
     */
    <#if controllerConfig.addPermission>
    @RequiresPermissions("${modelSnakeName!}:add")
    </#if>
    @RequestMapping("add")
    @ResponseBody
    public ResultBean add(${modelName!} ${modelHumpName!}){
        ResultBean result = new ResultBean();
        try {
            if (${modelHumpName!}==null){
                result.setSuccess(false).setMsg("保存失败，保存信息为空！");
            }else{
                ${modelHumpName!}Service.add(${modelHumpName!});
            }
        }catch (BusinessException e){
            result.setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            result.setSuccess(false).setMsg("系统异常，操作失败！");
            log.error("保存数据异常",e);
        }
        return result;
    }
</#if>

<#if controllerConfig.update>
    /**
     * 更新数据
     * @param ${modelHumpName!} 业务实体，id属性不能为空
     * @return
     */
    <#if controllerConfig.updatePermission>
    @RequiresPermissions("${modelSnakeName!}:edit")
    </#if>
    @RequestMapping("update")
    @ResponseBody
    public ResultBean update(${modelName!} ${modelHumpName!}){
        ResultBean result = new ResultBean();
        try {
            if (${modelHumpName!}==null || StringUtils.isEmpty(${modelHumpName!}.getId())){
                result.setSuccess(false).setMsg("请选择要更新的数据！");
            }else{
                ${modelHumpName!}Service.updateByPrimaryKey(${modelHumpName!},false);
            }
        }catch (BusinessException e){
            result.setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            result.setSuccess(false).setMsg("系统异常，操作失败！");
            log.error("更新数据异常",e);
        }
        return result;
    }
</#if>

<#if controllerConfig.delete>
    /**
     * 单个删除
     * @param id
     * @return
     */
    <#if controllerConfig.deletePermission>
    @RequiresPermissions("${modelSnakeName!}:delete")
    </#if>
    @RequestMapping("delete")
    @ResponseBody
    public ResultBean delete(@RequestParam(value = "id") Long id){
        ResultBean result = new ResultBean();
        try {
            if (StringUtils.isEmpty(id)){
                result.setSuccess(false).setMsg("请选择要删除的数据！");
            }else{
                ${modelHumpName!}Service.deleteByPrimaryKey(id);
            }
        }catch (BusinessException e){
            result.setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            result.setSuccess(false).setMsg("系统异常，操作失败！");
            log.error("删除数据异常",e);
        }
        return result;
    }
</#if>

<#if controllerConfig.batchDelete>
    /**
     * 批量删除，最多支持1000个一次
     * @param ids
     * @return
     */
    <#if controllerConfig.batchDeletePermission>
    @RequiresPermissions("${modelSnakeName!}:delete")
    </#if>
    @RequestMapping("batch_delete")
    @ResponseBody
    public ResultBean deleteBatch(@RequestParam(value = "ids") Long[] ids){
        ResultBean result = new ResultBean();
        try {
            if (ids==null || ids.length==0){
                result.setSuccess(false).setMsg("请选择要删除的数据！");
            }else if(ids.length>1000){
                result.setSuccess(false).setMsg("一次最多删除1000条记录！");
            }else{
                ${modelHumpName!}Service.batchDeleteByPrimaryKey(ids);
            }
        }catch (BusinessException e){
            result.setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            result.setSuccess(false).setMsg("系统异常，操作失败！");
            log.error("批量删除数据异常",e);
        }
        return result;
    }
</#if>
}
